//
//  YDTabBar.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/23.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit




class YDTabBar: UITabBar {
 
    //定义一个闭包
    var closure: (()->())?

    override init(frame framedfdfd: CGRect) {
        
        super.init(frame: framedfdfd)
        
     
        addSubview(addBtn)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()

        let btnW = bounds.width * 0.2
        let btnH = bounds.height
        var index: Int = 0
        //遍历子控件
        
        for sub in subviews{
        
            
            if sub.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                
                // 设置frame
                sub.frame = CGRectMake(CGFloat(index) * btnW, 0, btnW, btnH)
                
                index++
                
                if index == 2 {
                
                
                index++
                
                
                }
      
            
            }
            
        }
        
        addBtn.frame = CGRectMake(2*btnW, 0, btnW, btnH)

    }
    
    
    //懒加载
    lazy var addBtn: UIButton = {
    
        var btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.addTarget(self, action: "addBtnClick:", forControlEvents: .TouchUpInside)
        
        return btn
        
    
    
    }()
    
    
    
    //添加按钮点击方法
    
    func addBtnClick (btn: UIButton){
    
    
//        print("你点击了")
        
        self.closure?()
        
    
    }
    
    
    

}
